load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
;**************************************
begin

modelname = getenv("modelname")
varname = getenv("varname")
season = getenv("season")

;modelname = "ACCESS-CM2"
;varname = "pr30d"
;season = "DJF"

Nlevel = 6                 ;6 warming levels

Nindex = 6
pct_index = (/0.90,0.95,0.98,0.99,0.995,0.999/)        ;R90/R95/R98/R99/R99.5/R99.9


;----------------read------------------
diri := "/WORK/zhangwx/EConstraint/data/CMIP6/pr/"+varname+"/"+season+"/RX_pct_6index/"
fi := addfile(diri+"exceedance_RX_pct_"+varname+"_"+modelname+"_warming_levels_"+season+".nc","r")
exceedance_RX_pct := fi->exceedance_RX_pct        ;(6level 6index lat lon)    ;frq: unit =1


;----------------change in frq------------------
exceedance_RX_pct_base := exceedance_RX_pct(0,:,:,:)        ;(6index lat lon) [0.10,0.05,0.02,0.01,0.005,0.001]
exceedance_RX_pct_base_conf := conform(exceedance_RX_pct, exceedance_RX_pct_base, (/1,2,3/) )

change_frq_RX_pct := (exceedance_RX_pct - exceedance_RX_pct_base_conf)/exceedance_RX_pct_base_conf*100.    ;% change
copy_VarCoords(exceedance_RX_pct,change_frq_RX_pct)
change_frq_RX_pct@long_name = "% change in frequency wrt baseline (1995-2014)"
change_frq_RX_pct@units = "%"


;********************************************
filo := diri+"change_frequency_RX_pct_"+varname+"_"+modelname+"_warming_levels_"+season+".nc"
system("rm -f "+filo)
fo := addfile(filo,"c")
fo->change_frq_RX_pct = change_frq_RX_pct


print(varname+"_"+modelname+"_"+season+"  done")

end
